package com.zhengtoon.mctoon.conv.controller;

import com.bjtoon.framework.entity.ResponseResult;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.dto.CommentListQueryDto;
import com.zhengtoon.mctoon.conv.dto.mybatis.Page;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.service.ConvCommentService;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.vo.DelCommentVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 评论管理
 */
@RestController
@RequestMapping("/convphotoManage")
@Api(value = "convphotoComment服务", description = "convphotoComment服务")
public class ConvCommentController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ConvCommentController.class);

    @Autowired
    private ConvCommentService convCommentService;

    /**
     * 搜索评论
     *
     * @param commentListQueryDto
     * @return ResponseResult
     */
    @ApiOperation(value = "搜索评论 ", httpMethod = "POST", notes = "搜索评论")
    @RequestMapping(value = "/queryComment", method = RequestMethod.POST)
    public ResponseResult queryCommentList(
            @ApiParam(required = true, name = "commentListQueryDto", value = "commentListQueryDto") @RequestBody @Valid CommentListQueryDto commentListQueryDto) {
        try {
            // 1.参数校验
            if (commentListQueryDto.getPageNum() == null
                    || commentListQueryDto.getPageSize() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }

            Page page = new Page(commentListQueryDto.getPageNum(), commentListQueryDto.getPageSize());


            page.setParam(commentListQueryDto);

            // 2. 调用service接口
            Page pageResult = convCommentService.queryCommentList(page);

            // 3. 返回数据
            return ResponseResult.success(pageResult);
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("queryCommentList()方法error", e);
            return ResponseResult.fail("", "queryCommentList()方法error");
        }
    }

    /**
     * 显示/删除评论  144637
     *
     * @param delCommentVo
     * @return ResponseResult
     */
    @ApiOperation(value = "显示/删除评论 ", httpMethod = "POST", notes = "删除评论")
    @RequestMapping(value = "/delComment", method = RequestMethod.POST)
    public ResponseResult delComment(
            @ApiParam(required = true, name = "delCommentVo", value = "delCommentVo") @RequestBody @Valid DelCommentVo delCommentVo) {
        try {

            //1. 参数校验
            if (delCommentVo.getId() == null || delCommentVo.getIsPublic() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }

            // 2. 删除接口

            boolean success = convCommentService.delComment(delCommentVo.getId(), delCommentVo.getIsPublic());

            if (success) {
                return ResponseResult.success(success);
            }

            return ResponseResult.fail(AppConstant.FAIL_CODE, "显示/删除评论失败");
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("delComment()方法error", e);
            return ResponseResult.fail("", "delComment()方法error");
        }
    }

}